import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as es

from lib.button import *

from lib.share import css, xpath
from pom.basepage.basepage import BasePage,driver_get
from pom.page_teacher_op.homepage import THomePage
from pom.page_teacher_op.login_page import TLoginPage



class THomeworkCreatePage(THomePage):
    loc_title = (css,'.page-title')
    loc_homework_name = (xpath, "//*[@id='tbody_question_body']/*[1]/*[2]/input")
    loc_select_question = (css,'#exam_question_list .col-lg-12 button:nth-of-type(1)')
    loc_submit = (xpath,'//*[@class="btn btn-green ng-scope"]')

    #提示框
    loc_info_text = (css,".bootstrap-dialog-message")
    loc_info_button = (xpath,"//*[@class='bootstrap-dialog-message']/../../../*[3]/*[1]/*[1]/*[1]")

    #发布询问框
    loc_not_release = (xpath,"//*[@class='bootstrap-dialog-footer-buttons']/*[1]")
    loc_release_to_student = (xpath,"//*[@class='bootstrap-dialog-footer-buttons']/*[2]")

    #选择的习题(用于判断已选择习题的数量)
    loc_selected_questions = (xpath,"//*[@id='exam_question_list_choice']/div/div/div")

    loc_all_need_process = (css,'#btn_pick_question~button')
    loc_checkbox_need_process = (css,"[type='checkbox']")

    #暂不发布、发布给学生、
    loc_pop_not_release = (css, '.bootstrap-dialog-footer button:nth-of-type(1)')
    loc_pop_release = (css, '.bootstrap-dialog-footer button:nth-of-type(2)')

    loc_create_pop_text = (css, '.bootstrap-dialog-message img')

    #已选择题目的操作
    loc_question_op_check_value = "//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]/*[index1]/*[3]/*[2]/*[1]"
    loc_question_op_delete_value = "//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]/*[index1]/*[3]/*[2]/*[2]"
    loc_question_op_up_value = "//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]/*[index1]/*[3]/*[2]/*[3]"
    loc_question_op_down_value = "//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]/*[index1]/*[3]/*[2]/*[4]"

    #已选择题目的操作(多目标)
    loc_questions_op_check = (xpath,"//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]//*[@class='div-search-result-one-button-bar']/div/*[1]")
    loc_questions_op_delete = (xpath,"//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]//*[@class='div-search-result-one-button-bar']/div/*[2]")
    loc_questions_op_up = (xpath,"//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]//*[@class='div-search-result-one-button-bar']/div/*[3]")
    loc_questions_op_down = (xpath,"//*[@id='dynamicView']/*[3]/*[2]/*[1]/*[2]//*[@class='div-search-result-one-button-bar']/div/*[4]")

    def page_click(self):
        self.click(self.loc_title)

    def get_title(self):
        self.get_txt(self.loc_title)

    def homework_name_input(self,txt):
        self.input(self.loc_homework_name, txt)

    def select_question(self):
        self.click(self.loc_select_question)

    def submit(self):
        self.click(self.loc_submit)

class IFrameGreateHomework(THomePage):
    iframe_name = 'pick_questions_frame'

    loc_iframe_homework_info = (xpath, "//*[@class='txt']/..")

    loc_status = (xpath, "//*[@class='txt']/..//*[@id='cart_content']")

    # 状态栏所选的习题数
    # 选择题状态栏
    loc_status_choice = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[1]/span")
    loc_status_choice_easy = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[1]/ul/*[1]/span")
    loc_status_choice_middle = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[1]/ul/*[2]/span")
    loc_status_choice_hard = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[1]/ul/*[3]/span")
    # 填空题状态栏
    loc_status_blank = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[2]/span")
    loc_status_blank_easy = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[2]/ul/*[1]/span")
    loc_status_blank_middle = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[2]/ul/*[2]/span")
    loc_status_blank_hard = (xpath, "//*[@class='txt']/..//*[@id='cart_content']/ul/*[2]/ul/*[3]/span")
    # 状态栏集合
    status_dict = {
        '选择题': loc_status_choice,
        '选择题-简单': loc_status_choice_easy,
        '选择题-中等': loc_status_choice_middle,
        '选择题-较难': loc_status_choice_hard,
        '填空题': loc_status_blank,
        '填空题-简单': loc_status_blank_easy,
        '填空题-中等': loc_status_blank_middle,
        '填空题-较难': loc_status_blank_hard
    }

    # 多目标操作 ： 本页全移除、本页全加入、清空试题篮
    loc_all_del_page = (xpath, "//*[@class='txt']/../*[1]/*[2]/*[1]")
    loc_all_add_page = (xpath, "//*[@class='txt']/../*[1]/*[2]/*[2]")
    loc_all_clear = (xpath, "//*[@class='txt']/../*[1]/*[2]/*[3]")

    # 确定、取消
    loc_iframe_submit = (xpath, "//*[@class='txt']/../*[1]/*[2]/*[4]/*[2]")
    loc_iframe_cancel = (xpath, "//*[@class='txt']/../*[1]/*[2]/*[4]/*[1]")

    # 按章节  与  按知识点
    loc_kind_chapter = (xpath, "//*[@class='txt']/../*[2]/*[1]")
    loc_kind_point = (xpath, "//*[@class='txt']/../*[2]/*[2]")
    # 选择题  与  填空题
    loc_kind_choice = (xpath, "//*[@class='txt']/../*[3]/*[2]/*[1]")
    loc_kind_blank = (xpath, "//*[@class='txt']/../*[3]/*[2]/*[2]")
    # 浙教版  与  人教版
    loc_kind_zhejiaoban = (xpath, "//*[@class='txt']/../*[4]/*[1]/*[1]")
    loc_kind_renjiaoban = (xpath, "//*[@class='txt']/../*[4]/*[1]/*[2]")
    # 年级 ： 七年级、八年级、九年级、高一、高二、高三
    loc_grade_7 = (xpath, "//*[@class='txt']/../*[4]/*[2]/*[1]")
    loc_grade_8 = (xpath, "//*[@class='txt']/../*[4]/*[2]/*[2]")
    loc_grade_1 = (xpath, "//*[@class='txt']/../*[4]/*[2]/*[3]")
    loc_grade_h1 = (xpath, "//*[@class='txt']/../*[4]/*[2]/*[4]")
    loc_grade_h2 = (xpath, "//*[@class='txt']/../*[4]/*[2]/*[5]")
    loc_grade_h3 = (xpath, "//*[@class='txt']/../*[4]/*[2]/*[6]")
    # 册号 ： 上册、下册
    loc_book_1 = (xpath, "//*[@class='txt']/../*[4]/*[3]/*[1]")
    loc_book_2 = (xpath, "//*[@class='txt']/../*[4]/*[3]/*[2]")
    # 难度 ： 简单、中等、较难
    loc_easy = (xpath, "//*[@class='txt']/../*[4]/*[4]/*[1]")
    loc_middle = (xpath, "//*[@class='txt']/../*[4]/*[4]/*[2]")
    loc_hard = (xpath, "//*[@class='txt']/../*[4]/*[4]/*[3]")
    # 我的创建
    loc_my_created = (xpath, "//*[@class='txt']/../*[4]/*[5]/label")
    # 上一页、下一页
    loc_previous_page = (xpath, "//*[@class='pager']/*[1]/button")
    loc_next_page = (xpath, "//*[@class='pager']/*[2]/button")

    button_dict = {
        '按章节': loc_kind_chapter,
        '按知识点': loc_kind_point,
        '选择题': loc_kind_choice,
        '填空题': loc_kind_blank,
        '浙教版': loc_kind_zhejiaoban,
        '人教版': loc_kind_renjiaoban,
        '七年级': loc_grade_7,
        '八年级': loc_grade_8,
        '九年级': loc_grade_1,
        '高一': loc_grade_h1,
        '高二': loc_grade_h2,
        '高三': loc_grade_h3,
        '上册': loc_book_1,
        '下册': loc_book_2,
        '简单': loc_easy,
        '中等': loc_middle,
        '较难': loc_hard,
        '我创建的': loc_my_created,
        '本页全移除': loc_all_del_page,
        '本页全加入': loc_all_add_page,
        '清空试题篮': loc_all_clear,
        '确定': loc_iframe_submit,
        '取消': loc_iframe_cancel,
        '下一页':loc_next_page,
        '上一页':loc_previous_page
    }

    loc_pop = (css,'.bootstrap-dialog-footer button')
    loc_pop_text = (css,'.bootstrap-dialog-message')
    loc_no_search_text = (xpath,"//*[@class='alert alert-warning alert-dismissible']")

    def jump(self):
        self.jump_iframe(self.iframe_name)

    #获取状态栏试题数：选择题
    def get_status_choice(self):
        return self.get_txt(self.loc_status_choice)

    def get_status_choice_esay(self):
        return self.get_txt(self.loc_status_choice_easy)

    def get_status_choice_middle(self):
        return self.get_txt(self.loc_status_choice_middle)

    def get_status_choice_hard(self):
        return self.get_txt(self.loc_status_choice_hard)

    # 获取状态栏试题数：填空题
    def get_status_blank(self):
        return self.get_txt(self.loc_status_blank)

    def get_status_blank_esay(self):
        return self.get_txt(self.loc_status_blank_easy)

    def get_status_blank_middle(self):
        return self.get_txt(self.loc_status_blank_middle)

    def get_status_blank_hard(self):
        return self.get_txt(self.loc_status_blank_hard)

    def get_all_status(self):
        status_count_dict = {}
        for key,loc in self.status_dict.items():
            status_count_dict[key] = self.get_txt(loc)
        return status_count_dict

    def get_status_append_to_list(self,get_status_list):
        res_list = []
        all_status = self.get_all_status()
        for i in get_status_list:
            res_list.append(all_status[i])
        return res_list

    #章节树
    #章节树title
    loc_tree_title = (xpath,"//*[@class='txt']/../*[5]/*[1]/ul/li/*[3]/i")

    loc_tree_chapter_page_foots = (xpath,"//*[@class='txt']/../*[5]/*[1]/ul/li/*[4]/li/i")
    loc_tree_chapter_method = xpath
    loc_tree_chapter_value1 = "//*[@class='txt']/../*[5]/*[1]/ul/li/*[4]/*[index1]/a"
    loc_tree_chapter_value2 = "/../ul/*[index2]"
    loc_tree_chapter_value_list = [loc_tree_chapter_value1,loc_tree_chapter_value2]

    # 知识点树
    loc_tree_point_method = xpath
    loc_tree_point_value1 = "//*[@class='flex-row-sb']/*[1]/*[1]/*[1]/*[4]/*[index1]/a"
    loc_tree_point_value2 = "/../ul/*[index2]/a"
    loc_tree_point_value3 = "/../ul/*[index3]"
    loc_tree_point_value_list = [loc_tree_point_value1,loc_tree_point_value2,loc_tree_point_value3]

    #点击上一页
    def click_previous_page(self):
        self.click(self.loc_previous_page)

    #点击下一页
    def click_next_page(self):
        self.click(self.loc_next_page)

    #题目     #diff:难度
    loc_question_method = xpath
    loc_question_value = "//*[@id='serach_result_table']/*[index1]"
    loc_question_diff_value = "//*[@id='serach_result_table']/*[index1]/*[1]"
    loc_question_content_value = "//*[@id='serach_result_table']/*[index1]/*[2]"
    loc_question_check_value = "//*[@id='serach_result_table']/*[index1]/*[3]/*[1]/*[1]"
    loc_question_add_value = "//*[@id='serach_result_table']/*[index1]/*[3]/*[1]/*[2]"
    loc_question_delete_value = "//*[@id='serach_result_table']/*[index1]/*[3]/*[1]/*[3]"

    question_buttin_dict = {
            button_查看: loc_question_check_value,
            button_加入试题篮: loc_question_add_value,
            button_移出试题篮: loc_question_delete_value
    }

    def click_question(self,q_index,button):
        loc_value_input = self.question_buttin_dict[button]
        ret = self.click_floor(xpath,loc_value_input,index1=q_index)
        return ret

class QuestionCheck(BasePage):
    pass

if __name__ == "__main__":
    # driver = webdriver.Chrome()
    driver = driver_get('Chrome')
    tloginpage = TLoginPage(driver)
    tloginpage.open(tloginpage.t_url)
    tloginpage.login()

    thomepage = THomePage(driver)
    thomepage.open_homework_create()

    thomeworkcreatepage = THomeworkCreatePage(driver)
    thomeworkcreatepage.page_click()
    time.sleep(1)
    thomeworkcreatepage.homework_name_input('作业1')

    # thomeworkcreatepage.submit()
    # time.sleep(5)

    thomeworkcreatepage.select_question()

    # for loc in thomeworkcreatepage.loc_status_list:
    #     status_text = thomeworkcreatepage.get_txt(loc)
    #     print(status_text)

    # thomeworkcreatepage.get_txt(thomeworkcreatepage.loc_status_choice)
    iframe_create_homework = IFrameGreateHomework(driver)
    iframe_create_homework.jump()
    # time.sleep(5)
    # print(iframe_create_homework.get_status_choice())
    # print(iframe_create_homework.get_status_choice_esay())
    # print(iframe_create_homework.get_status_choice_middle())
    # print(iframe_create_homework.get_status_choice_hard())
    # print(iframe_create_homework.get_status_blank())
    # print(iframe_create_homework.get_status_blank_esay())
    # print(iframe_create_homework.get_status_blank_middle())
    # print(iframe_create_homework.get_status_blank_hard())
    # print(iframe_create_homework.get_all_status())

    #多按钮check
    # clicklist = [button_按知识点,button_按章节,button_填空题,button_选择题,button_人教版,button_浙教版,
    #              button_九年级,button_八年级,button_高三,button_高二,button_高一,
    #              button_七年级,button_中等,button_较难,button_简单,button_简单,button_下册,
    #              button_上册,button_本页全加入,button_本页全移除,button_本页全加入,button_清空试题篮,
    #              button_我创建的]
    # iframe_create_homework.click_buttons(clicklist)



    time.sleep(5)

    driver.quit()






